package com.lj.back.manage.scaffold.module.job.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lj.back.manage.scaffold.common.DTO.PageQuery;
import com.lj.back.manage.scaffold.common.response.R;
import com.lj.back.manage.scaffold.common.util.CommonUtil;
import com.lj.back.manage.scaffold.module.job.DTO.ScheduleJobDTO;
import com.lj.back.manage.scaffold.module.job.DTO.ScheduleJobOperate;
import com.lj.back.manage.scaffold.module.job.DTO.ScheduleJobUpdate;
import com.lj.back.manage.scaffold.module.job.DTO.ScheduleJobUpdateOwner;
import com.lj.back.manage.scaffold.module.job.VO.ScheduleJobVO;
import com.lj.back.manage.scaffold.module.job.service.ScheduleJobService;
import com.lj.back.manage.scaffold.module.sys.annotation.SysLog;
import com.lj.back.manage.scaffold.module.sys.enums.OperateTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author lv
 */
@RestController
@RequestMapping("/schedule/job")
@Api(tags = "定时任务模块")
@ApiSupport(author = "lv")
@AllArgsConstructor
public class ScheduleJobController {

	private final ScheduleJobService scheduleJobService;

	@GetMapping("/list")
    @ApiOperation(value = "分页查询定时任务列表")
    @ApiOperationSupport(order = 1)
    @RequiresPermissions("sys:job:list")
	public R<Object> listJobs(@Validated PageQuery pageQuery){
        Page<ScheduleJobDTO> jobPage = scheduleJobService.listJobs(pageQuery);
        IPage<ScheduleJobVO> jobVoPage = CommonUtil.pageBeanConvert(jobPage, ScheduleJobVO.class);
        return R.success(jobVoPage);
	}

	@GetMapping("/info/{id}")
    @ApiOperation(value = "根据ID查询定时任务详情")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParam(name = "id", value = "定时任务ID", example = "1", required = true, dataTypeClass = Long.class)
    @RequiresPermissions("sys:job:select")
	public R<Map<String,Object>> getJobInfo(@PathVariable("id") String id){
        ScheduleJobDTO scheduleJobDTO = scheduleJobService.getScheduleJobInfo(id);
        ScheduleJobVO scheduleJobVO = BeanUtil.copyProperties(scheduleJobDTO, ScheduleJobVO.class);
        return R.success().build("job", scheduleJobVO);
	}

	@PatchMapping("/update")
    @SysLog(value = "更新定时任务",operateType = OperateTypeEnum.UPDATE)
    @ApiOperation(value = "更新定时任务")
    @ApiOperationSupport(order = 3)
    @RequiresPermissions("sys:job:update")
	public R<Object> updateJob(@RequestBody @Validated ScheduleJobUpdate scheduleJobUpdate){
        ScheduleJobDTO scheduleJobDTO = BeanUtil.copyProperties(scheduleJobUpdate, ScheduleJobDTO.class);
        scheduleJobService.updateScheduleJobCheckOwner(scheduleJobDTO);
        return R.success();
    }

    @PatchMapping("/updateJobOwner")
    @SysLog(value = "更新定时任务拥有者",operateType = OperateTypeEnum.UPDATE)
    @ApiOperation(value = "更新定时任务拥有者")
    @ApiOperationSupport(order = 4)
    @RequiresPermissions("sys:job:admin")
    public R<Object> updateJobOwner(@RequestBody @Validated ScheduleJobUpdateOwner scheduleJobUpdate){
        ScheduleJobDTO scheduleJobDTO = BeanUtil.copyProperties(scheduleJobUpdate, ScheduleJobDTO.class);
        scheduleJobService.updateScheduleJob(scheduleJobDTO);
        return R.success();
    }

    @PatchMapping("/operate")
    @SysLog(value = "操作定时任务",operateType = OperateTypeEnum.UPDATE)
    @ApiOperation(value = "操作定时任务")
    @ApiOperationSupport(order = 5)
    @RequiresPermissions("sys:job:operate")
    public R<Object> operateJob(@RequestBody @Validated ScheduleJobOperate scheduleJobOperate){
        ScheduleJobDTO scheduleJobDTO = BeanUtil.copyProperties(scheduleJobOperate, ScheduleJobDTO.class);
        scheduleJobService.updateScheduleJobCheckOwner(scheduleJobDTO);
        return R.success();
    }
}
